package cn.sxt.logistics.shiro;

import cn.sxt.logistics.mapper.AdminMapper;
import cn.sxt.logistics.mapper.PermissionMapper;
import cn.sxt.logistics.pojo.Admin;
import cn.sxt.logistics.pojo.Permission;
import cn.sxt.logistics.pojo.Role;
import cn.sxt.logistics.pojo.RolePermission;
import cn.sxt.logistics.service.IAdminService;
import cn.sxt.logistics.service.IRolePermissionService;
import cn.sxt.logistics.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;


public class CustomRealm extends AuthorizingRealm {

    @Autowired
    @Lazy
    private IAdminService adminService;

    @Autowired
    @Lazy
    private IRoleService roleService;

    @Autowired
    @Lazy
    private PermissionMapper permissionMapper;


    /*获取授权信息方法*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //1.获取当前认证身份
        Admin admin = (Admin) principals.getPrimaryPrincipal();

        //2.查询当前身份的角色拥有的权限表达式
        List<String> expressions = permissionMapper.selectExpressionsByRoleId(admin.getRoleId());
        for (String expression : expressions) {
            System.out.println("expression ："+expression);
        }
        //3.创建授权信息对象
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        //3.1将权限表达式和当前身份的角色添加到授权信息对象
        authorizationInfo.addRole(admin.getRolename());
        authorizationInfo.addStringPermissions(expressions);

        return authorizationInfo;
    }
    /*获取认证信息方法*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户登录的身份(账号)
        String username = (String) token.getPrincipal();

        //从数据库中查询是否有此用户
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Admin admin = adminService.getOne(queryWrapper);

        //不存在：返回null，Shiro底层会抛 UnknownAccountException
        if(admin == null){
            return  null;
        }
        //查询出用户的角色
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_id",admin.getRoleId());
        Role role = roleService.getOne(roleQueryWrapper);
        //将查询的角色设置给Admin
        admin.setRole(role);
        admin.setRolename(role.getRolename());


        //获取密码
        String password = admin.getPassword();

        //创建认证信息对象:明文
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(admin, password, this.getName());

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(admin, password, ByteSource.Util.bytes(admin.getSalt()), this.getName());

        System.out.println("adminService :" +adminService);
        System.out.println("====================");
        return authenticationInfo;
    }
}
